<?php

class DocumentController extends Controller
{
	/**
	 * @var string the default layout for the views. Defaults to '//layouts/column2', meaning
	 * using two-column layout. See 'protected/views/layouts/column2.php'.
	 */
	public $layout='//layouts/column1';

	/**
	 * @return array action filters
	 */
	public function filters()
	{
		return array(
			'accessControl', // perform access control for CRUD operations
		);
	}

	/**
	 * Specifies the access control rules.
	 * This method is used by the 'accessControl' filter.
	 * @return array access control rules
	 */
	public function accessRules()
	{
		return array(
			array('allow',  // allow all users to perform 'index' and 'view' actions
				'actions'=>array('index','view'),
				'users'=>array('*'),
			),
			array('allow', // allow authenticated user to perform 'create' and 'update' actions
				'actions'=>array('create','update'),
				'users'=>array('@'),
			),
			array('allow', // allow admin user to perform 'admin' and 'delete' actions
				'actions'=>array('admin','delete'),
				'users'=>array('admin'),
			),
			array('deny',  // deny all users
				'users'=>array('*'),
			),
		);
	}

	/**
	 * Lists all models.
	 */
	public function actionIndex()
	{
	  $subjectID = isset($_GET['subjectID'])?$_GET['subjectID']:'';
	  $subject = array();
	  $where = "";
		$model = new Document('search_doc');
		$model->unsetAttributes();  // clear any default values
	  if($subjectID!="") {
      Yii::import('application.controllers.SubjectController');
  		$controller_subject = new SubjectController("Subject");
  		$subject = $controller_subject->loadModel($subjectID); 
  		$where = " AND tp.subjectID=$subject->ID";
    }
		$doc = Yii::app()->db->createCommand()
		    ->select('t.*,tp.subjectID,sub.subject,t.topicID,tp.name as topic')
		    ->from('tbl_document t')
		    ->join('tbl_topic tp', 'tp.ID=t.topicID AND tp.deleted=0')
		    ->join('tbl_subject sub','sub.ID=tp.subjectID AND sub.deleted=0')
		    ->join('tbl_grade gr','gr.ID=tp.gradeID AND gr.deleted=0')
		    ->where('t.deleted=0 AND t.enabled=1'.$where)
		    ->order('sub.ID, topic, t.createddate DESC')
		    ->queryAll();
		$array = array();
		if(!empty($doc)) {
		  foreach ($doc as $d) {
		    if($subjectID!="") {
  		    if(!array_key_exists($d['topicID'], $array)) {
  		      $array[$d['topicID']] = array('topicID'=>$d['topicID'],'topic'=>$d['topic'],array('ID'=>$d['ID'],'name'=>$d['name'],'description'=>$d['description'],'content'=>$d['content'],'photo'=>$d['photo']));
  		    }
  		    else {
  		      array_push($array[$d['topicID']], array('ID'=>$d['ID'],'name'=>$d['name'],'description'=>$d['description'],'content'=>$d['content'],'photo'=>$d['photo']));
  		    }
		    }
		    else {
  		    if(!array_key_exists($d['subjectID'], $array)) {
  		      $array[$d['subjectID']] = array('subjectID'=>$d['subjectID'],'subject'=>$d['subject'],array('ID'=>$d['ID'],'name'=>$d['name'],'description'=>$d['description'],'content'=>$d['content'],'photo'=>$d['photo']));
  		    }
  		    else {
  		      array_push($array[$d['subjectID']], array('ID'=>$d['ID'],'name'=>$d['name'],'description'=>$d['description'],'content'=>$d['content'],'photo'=>$d['photo']));
  		    }
		    }
		  }
		}
		//d($array); die;
		$this->render('index',array(
			'model'=>$model, 'array'=>$array, 'subjectID'=>$subjectID, 'subject'=>$subject
		));
	}

	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 * @param integer the ID of the model to be loaded
	 */
	public function loadModel($id)
	{
		$model=Document::model()->findByPk($id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}
}
